{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5662475e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5c44e0b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2022-07-18 00:00:00')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime('2022-7-18') # to_datetime将字符串转为时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "da8ab430",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                  2022\n",
       "1               2021-12\n",
       "2             2020/11/5\n",
       "3    2019-10-1 12:24:05\n",
       "dtype: object"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = pd.Series(['2022', '2021-12', '2020/11/5'\n",
    "               , '2019-10-1 12:24:05'])\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6b896338",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2022-01-01 00:00:00\n",
       "1   2021-12-01 00:00:00\n",
       "2   2020-11-05 00:00:00\n",
       "3   2019-10-01 12:24:05\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将Series的类型转为日期时间类型\n",
    "s2 = pd.to_datetime(s1)\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c240ef12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     1\n",
       "1    12\n",
       "2    11\n",
       "3    10\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.dt.month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "56cbd677",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     True\n",
       "1     True\n",
       "2    False\n",
       "3     True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.dt.is_month_start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d00e7b80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2022-01-01 00:00:00\n",
       "1   2021-12-01 00:00:00\n",
       "3   2019-10-01 12:24:05\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2[s2.dt.is_month_start]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e3aa6fcf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timedelta('8 days 00:00:00')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt1 = pd.to_datetime('2022-7-18')\n",
    "dt2 = pd.to_datetime('2022-7-10')\n",
    "delta_dt = dt1 - dt2   # 获取 dt1 和 dt2 的时间差，返回Timedelta\n",
    "delta_dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b0fb9b92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   18992 days 23:59:59.999998\n",
       "1   18961 days 23:59:59.999998\n",
       "2   18570 days 23:59:59.999998\n",
       "3   18170 days 12:24:04.999998\n",
       "dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3 = s2 - pd.to_datetime(2000)\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8700b909",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',\n",
       "               '2022-01-05', '2022-01-06', '2022-01-07'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2022-1-1', periods=7)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "47dbb0ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2022-01-02', '2022-01-09', '2022-01-16', '2022-01-23',\n",
       "               '2022-01-30', '2022-02-06', '2022-02-13'],\n",
       "              dtype='datetime64[ns]', freq='W-SUN')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2022-1-1', periods=7, freq='W')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64104070",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
